package com.zhh.springcloud.controller;

import com.zhh.springcloud.model.Department;
import com.zhh.springcloud.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//服务提供者
@RestController
@RequestMapping("/provider")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/add")
    public int add(@RequestBody Department department){
        return departmentService.add(department);
    }

    @GetMapping("/delete")     // 这里的id是限制前端表单上传的变量名称
    public int delete(@RequestParam(value = "id", required = false) int did){
        return departmentService.deleteById(did);
    }

    @PostMapping("/modify")
    public int modify(@RequestBody Department department){
        return departmentService.modifyById(department);
    }

    @GetMapping("/get")
    public Department getById(@RequestParam(value = "id", required = false) int did){
        return departmentService.selectById(did);
    }

    @GetMapping("/getall")
    public List<Department> getAll(){
        return departmentService.selectAll();
    }

    //给微服务监控提供一些接口
    public Object discovery(){
        //获取微服务列表的清单
        List<String> services = discoveryClient.getServices();
        System.out.printf("discovery=>services:" + services);

        //得到一个具体的微服务信息
        //........
        return new Object();
    }
}
